<template>
  <div class="layout-all">
    <!-- <control :config="config" /> -->
    <!-- <div class="layout-header" style="float:left" /> -->
    <div style="margin-left:20px;color:red;margin-top:5px;font-size:20px;font-weight:bold;">
      温馨提示：金额比例请勿擅自修改
    </div>
     <el-card  style="width:100%;margin-top:20px;">
        <div slot="header" class="clearfix">
          <span>金额参数</span>

        </div>
    <div class="layout-content">
      <el-table
        :data="tableData"
        stripe
        align="center"
        highlight-current-row
        style="width: 100%"
      >
        <el-table-column align="center" prop="title" label="标题" />
        <el-table-column align="center" prop="scale" label="使用比例" width="150" />

        <el-table-column align="center" prop="is_withdraw" label="是否提现" width="80">
          <template slot-scope="scope">
            <el-button
              :type="scope.row.is_withdraw === 1 ? 'primary' : 'success'"
              size="mini"
              @click="change_status(scope.row.id,'is_withdraw' ,scope.row.is_withdraw)"
            >{{ scope.row.is_withdraw === 1 ? "是" : "否" }}</el-button>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="withdraw_scale" label="提现比例" width="120" />
        <el-table-column align="center" prop="is_recharge" label="是否充值" width="80">
          <template slot-scope="scope">
            <el-button
              :type="scope.row.is_recharge === 1 ? 'primary' : 'success'"
              size="mini"
              @click="change_status(scope.row.id,'is_recharge' ,scope.row.is_recharge)"
            >{{ scope.row.is_recharge === 1 ? "是" : "否" }}</el-button>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="recharge_scale" label="充值比例" width="120" />
        <el-table-column align="center" prop="is_grant" label="是否购买发放" width="120">
          <template slot-scope="scope">
            <el-button
              :type="scope.row.is_grant === 1 ? 'primary' : 'success'"
              size="mini"
              @click="change_status(scope.row.id,'is_grant' ,scope.row.is_grant)"
            >{{ scope.row.is_grant === 1 ? "是" : "否" }}</el-button>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="grant_scale" label="发放比例" width="120" />
        <el-table-column align="center" prop="status" label="是否启用" width="80">
          <template slot-scope="scope">
            <el-button
              :type="scope.row.status === 1 ? 'primary' : 'success'"
              size="mini"
              @click="change_status(scope.row.id,'status' ,scope.row.status)"
            >{{ scope.row.status === 1 ? "是" : "否" }}</el-button>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="min_use" label="最低使用(商品单独设置除外)" width="120" />
        <el-table-column align="center" prop="field_str" label="字段" width="120" />
        <el-table-column
          align="center"
          prop="telephone"
          :fixed="isMobile()?undefined:'right'"
          width="240"
          label="操作"
        >
          <template slot-scope="scope">
            <router-link :to="'./form/' + scope.row.id">
              <el-button size="small" type="primary" icon="el-icon-edit" />
            </router-link>
            <!-- <el-button
              type="danger"
              icon="el-icon-delete"
              @click="onDel(scope.row.id)"
            /> -->
          </template>
        </el-table-column>
      </el-table>
    </div>
    </el-card>
    <div style="margin-top:20px;">
      <el-card  class="form-width">
        <div slot="header" class="clearfix">
          <span>平台分佣设置</span>
          <el-button style="float: right; padding: 3px 0;" type="text" @click="save()">保存</el-button>
        </div>
        <div >
          <el-form ref="form"  label-width="130px" >
            <el-radio-group style="margin-left:40px;margin-bottom:20px;" v-model="proportion.commission_type" >
                <el-radio-button  :label="0">按比例分佣</el-radio-button>
                <el-radio-button :label="1">按金额分佣</el-radio-button>
            </el-radio-group>
              <el-form-item :label="(index+1)+'级分佣比(%):'" prop="first_rebate" v-for="(val,index) in proportion.proportion" :key="index" v-if="proportion.commission_type==0">
                <el-input v-model="proportion.proportion[index]"  placeholder="请输入一级分佣比(%)" class="form-width-n"/>
              </el-form-item>
              <el-form-item :label="(index+1)+'级分佣金额(元):'" prop="first_rebate" v-for="(val,index) in proportion.fixed" :key="index" v-if="proportion.commission_type==1">
                <el-input v-model="proportion.fixed[index]"  placeholder="请输入一级分佣比(元)" class="form-width-n"/>
              </el-form-item>
            </el-form>
        </div>
      </el-card>


    </div>
    <!-- <el-pagination
      :current-page="current_page"
      :page-size="per_page"
      layout="total, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    /> -->
  </div>
</template>

<script>
// import control from '../../components/control/control'
export default {
  name:'system_index',
  // components: { control },
  data() {
    return {
      config: [
        { type: 'text', placeholder: '请输入标题', field: 'title', val: '' }
      ],
      value: '',
      searchWhere: {
        title: ''
      },
      tableData: [],
      RETAIL_LEVEL: 0,
      proportion: {
        id:1,
        commission_type:0,
        fixed:[],
        proportion:[],
      },
      total: 0,
      current_page: 1,
      per_page: 14
    }
  },
  activated(){
    if(this.$getUpdate()){
      this.onSearch()
    }
  },
  created() {
    this.onSearch()
  },
  methods: {
    // 查询
    handleSizeChange(val) {
      this.onSearch()
    },
    handleCurrentChange(val) {
      this.current_page = val
      this.onSearch()
    },
    onSearch() {
      const form = {}
      form.page = this.current_page
      form.map = this.searchWhere
      this.get('/admin/MoneyScaleRules/index', form, 'POST').then(e => {
        this.tableData = e.list
        this.proportion=e.proportion
        this.RETAIL_LEVEL=e.RETAIL_LEVEL
        if(e.proportion!=null){
          this.proportion=e.proportion
          //this.proportion.commission_type	=this.proportion.commission_type== 1 ?true:false
        }
        let count=this.RETAIL_LEVEL-this.proportion.fixed.length
        if(count<0){
          this.proportion.fixed=this.proportion.fixed.slice(0,this.RETAIL_LEVEL)
        }else if(count>0){
          for(let i=0 ;i<count;i++){
            this.proportion.fixed.push(0)
          }
        }
        count=this.RETAIL_LEVEL-this.proportion.proportion.length
        if(count<0){
          this.proportion.proportion=this.proportion.proportion.slice(0,this.RETAIL_LEVEL)
        }else if(count>0){
          for(let i=0 ;i<count;i++){
            this.proportion.proportion.push(0)
          }
        }

        // this.per_page = e.list.per_page
        // this.total = e.list.total
        // this.current_page = e.list.current_page
      })
    },
    save(){
      let from=this.proportion
      this.get('/admin/Commission/save', from, 'POST').then(e => {
        this.$message({
            message: '修改成功',
            type: 'success'
          });
      })

    },
    // 添加
    onAdd() {},
    // 编辑
    onEdit(id) {
      console.log(id)
    },
    // 修改状态
    change_status(id, field, val) {
      this.update(id, val, field, 'MoneyScaleRules').then(e => {
        e ? this.onSearch() : ''
      })
    },
    // 删除
    onDel(id) {
      this.$msg_confirm().then(e => {
        if (e) {
          this.get('/admin/MoneyScaleRules/del?id=' + id, this.form, 'POST').then(e => {
            this.onSearch()
          })
        }
      })
    }
  }
}
</script>

<style>
.layout-all {
  margin-top: 20px;
  margin-left: 10px;
}
.layout-content {
  margin-top: 0px;
}
</style>>
